package com.tsd.jxc.sale.vo;

import com.alibaba.fastjson.JSON;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.fin.vo.PaymentRequestExportVo;
import com.tsd.jxc.goods.dao.JxcGoodsMapper;
import com.tsd.jxc.goods.vo.JxcGoodsVo;
import com.tsd.jxc.sale.entity.JxcSaleOrderDetailExt;
import com.tsd.jxc.sale.entity.JxcSaleOrderExt;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.dao.SysSettingMapper;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.entity.SysUser;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: JxcSaleOrderExportVo
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2023/3/21 17:17
 * @Version: 1.0
 */
public class JxcSaleOrderExportVo {

    private String client_display;

    private String client_linker_name;

    private String client_linker_mobile;

    private String client_linker_address;

    private String contract_person_name;

    private String contract_person_phone;

    private String client_address;

    private String code;

    private Date order_date;

    private long order_date_long;

    private BigDecimal total_qty;

    private BigDecimal total_amount;

    private String company_name;

    private String linker_name;

    private String linker_phone;

    private String linker_address;

    private List<JxcSaleOrderDetailExportVo> detail_list;

    private Integer size;

    private Map<String, Object> size_map;

    private List<Map<String, Object>> size_list;

    public String getClient_display() {
        return client_display;
    }

    public void setClient_display(String client_display) {
        this.client_display = client_display;
    }

    public String getClient_linker_name() {
        return client_linker_name;
    }

    public void setClient_linker_name(String client_linker_name) {
        this.client_linker_name = client_linker_name;
    }

    public String getClient_linker_mobile() {
        return client_linker_mobile;
    }

    public void setClient_linker_mobile(String client_linker_mobile) {
        this.client_linker_mobile = client_linker_mobile;
    }

    public String getClient_linker_address() {
        return client_linker_address;
    }

    public void setClient_linker_address(String client_linker_address) {
        this.client_linker_address = client_linker_address;
    }

    public String getContract_person_name() {
        return contract_person_name;
    }

    public void setContract_person_name(String contract_person_name) {
        this.contract_person_name = contract_person_name;
    }

    public String getContract_person_phone() {
        return contract_person_phone;
    }

    public void setContract_person_phone(String contract_person_phone) {
        this.contract_person_phone = contract_person_phone;
    }

    public String getClient_address() {
        return client_address;
    }

    public void setClient_address(String client_address) {
        this.client_address = client_address;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Date getOrder_date() {
        return order_date;
    }

    public void setOrder_date(Date order_date) {
        this.order_date = order_date;
    }

    public long getOrder_date_long() {
        return order_date_long;
    }

    public void setOrder_date_long(long order_date_long) {
        this.order_date_long = order_date_long;
    }

    public BigDecimal getTotal_qty() {
        return total_qty;
    }

    public void setTotal_qty(BigDecimal total_qty) {
        this.total_qty = total_qty;
    }

    public BigDecimal getTotal_amount() {
        return total_amount;
    }

    public void setTotal_amount(BigDecimal total_amount) {
        this.total_amount = total_amount;
    }

    public String getCompany_name() {
        return company_name;
    }

    public void setCompany_name(String company_name) {
        this.company_name = company_name;
    }

    public String getLinker_name() {
        return linker_name;
    }

    public void setLinker_name(String linker_name) {
        this.linker_name = linker_name;
    }

    public String getLinker_phone() {
        return linker_phone;
    }

    public void setLinker_phone(String linker_phone) {
        this.linker_phone = linker_phone;
    }

    public String getLinker_address() {
        return linker_address;
    }

    public void setLinker_address(String linker_address) {
        this.linker_address = linker_address;
    }

    public List<JxcSaleOrderDetailExportVo> getDetail_list() {
        return detail_list;
    }

    public void setDetail_list(List<JxcSaleOrderDetailExportVo> detail_list) {
        this.detail_list = detail_list;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Map<String, Object> getSize_map() {
        return size_map;
    }

    public void setSize_map(Map<String, Object> size_map) {
        this.size_map = size_map;
    }

    public List<Map<String, Object>> getSize_list() {
        return size_list;
    }

    public void setSize_list(List<Map<String, Object>> size_list) {
        this.size_list = size_list;
    }

    public static JxcSaleOrderExportVo transfer(JxcSaleOrderExt orderExt, JxcSettingVo settingVo, SysUser opUser) {
        if (orderExt == null) {
            return null;
        }
        JxcSaleOrderExportVo exportVo = new JxcSaleOrderExportVo();
        exportVo.setClient_display(orderExt.getClient_display());
        exportVo.setClient_linker_name(orderExt.getClient_linker_name());
        exportVo.setClient_linker_mobile(orderExt.getClient_linker_mobile());
        exportVo.setClient_linker_address(orderExt.getClient_linker_address());
        exportVo.setContract_person_name(orderExt.getContract_person_name());
        exportVo.setContract_person_phone(orderExt.getContract_person_phone());
        exportVo.setClient_address(orderExt.getClient_address());
        exportVo.setCode(orderExt.getCode());
        exportVo.setOrder_date(orderExt.getOrder_date());
        exportVo.setOrder_date_long(orderExt.getOrder_date().getTime());
        exportVo.setTotal_qty(orderExt.getQty());
        exportVo.setTotal_amount(orderExt.getAmount());
        SysSettingMapper sysSettingMapper = SpringContextUtil.getBean(SysSettingMapper.class);
        SysSettingExt settingExt = sysSettingMapper.selectByCodeOwnerSid(new SysSettingExt(opUser.getSid()));
        if (settingExt != null && !HlpUtils.isEmpty(settingExt.getJson())) {
            PaymentRequestExportVo vo = JSON.parseObject(settingExt.getJson(), PaymentRequestExportVo.class);
            exportVo.setCompany_name(vo.getCompany_name());
            exportVo.setLinker_name(vo.getLinker_name());
            exportVo.setLinker_phone(vo.getLinker_phone());
            exportVo.setLinker_address(vo.getLinker_address());
        }
        List<JxcSaleOrderDetailExt> detailExtList = orderExt.getDetail_list();
        List<JxcSaleOrderDetailExportVo> voList = new ArrayList<>();
        if (!HlpUtils.isEmptyList(detailExtList)) {
            JxcGoodsMapper jxcGoodsMapper = SpringContextUtil.getBean(JxcGoodsMapper.class);
            Map<String, Object> params = new HashMap<>();
            params.put("goods_list", ListUtil.map(detailExtList, JxcSaleOrderDetailExt::getGoods_sid));
            List<JxcGoodsVo> goodsVoList = jxcGoodsMapper.select4ClothingByPage(params, null);
            List<String> unionSizeList = new ArrayList<>();
            for (JxcGoodsVo paramsVo : goodsVoList) {
//                List<String> filterSizeList = ListUtil.filter(paramsVo.getSpec_preset_value_list(), t -> !unionSizeList.contains(t));
//                if (!HlpUtils.isEmptyList(filterSizeList)) {
//                    unionSizeList.addAll(filterSizeList);
//                }
            }
            List<String> sizeGroupList = ClothingUtil.getClothing_preset_size_group_list(settingVo);
            List<String> filterSizeGroupList = ListUtil.filter(sizeGroupList, sizeGroup -> ListUtil.anyMatch(unionSizeList, t -> String.format(",%s,", sizeGroup).contains(String.format(",%s,", t))));
            int maxSize = 0;
            for (String sizeGroup : filterSizeGroupList) {
                int len = sizeGroup.split(",").length;
                if (maxSize < len) {
                    maxSize = len;
                }
            }
            exportVo.setSize(maxSize);
            Map<String, Object> sizeMap = new HashMap<>();
            Map<String, Object> sizeMap2 = new HashMap<>();
            for (int i = 0; i < maxSize; i++) {
                List<String> sizeStrList = new ArrayList<>();
                for (String sizeGroup : filterSizeGroupList) {
                    String[] strings = sizeGroup.split(",");
                    if (i < strings.length) {
                        sizeStrList.add(strings[i]);
                    }
                }
                sizeMap2.put("preset_size_" + i, StringUtils.join(sizeStrList, "/"));
                sizeMap.put("preset_size_" + i, StringUtils.join(sizeStrList, "/"));
                String qtyField = "t.preset_qty_map.preset_qty_" + i;
                if (sizeStrList.size() == 1) {
                    qtyField = "n:" + qtyField;
                }
                sizeMap.put("preset_qty_" + i, qtyField);
            }
            sizeMap.put("qty", "n:t.qty");
            sizeMap.put("price", "n:t.price");
            sizeMap.put("amount", "n:t.amount");
            sizeMap.put("note_info", "t.note_info}}");
            exportVo.setSize_map(sizeMap2);
            exportVo.setSize_list(Collections.singletonList(sizeMap));
            for (JxcGoodsVo goodsVo : goodsVoList) {
                List<JxcSaleOrderDetailExt> filterDetailList = null;//ListUtil.filter(detailExtList, t -> goodsVo.containsProduct(t.getProduct_sid()));
                if (HlpUtils.isEmptyList(filterDetailList)) {
                    continue;
                }
                JxcSaleOrderDetailExportVo detailExportVo = new JxcSaleOrderDetailExportVo();
                detailExportVo.setGoods_code(goodsVo.getCode());
                detailExportVo.setGoods_name(goodsVo.getName());
//                detailExportVo.setColor_name(goodsVo.getClothing_color_name());
//                detailExportVo.setSpec_name(goodsVo.getSpec_name());
                detailExportVo.setPrice(filterDetailList.get(0).getPrice());
                detailExportVo.setSn(filterDetailList.get(0).getSn());
                Map<String, String> presetProductMap = null;//goodsVo.getClothing_preset_product_map();
                Map<String, String> presetQtyMap = new HashMap<>();
                for (int i = 0; i < maxSize; i++) {
                    List<String> qtyStrList = new ArrayList<>();
                    for (String sizeGroup : filterSizeGroupList) {
                        String[] strings = sizeGroup.split(",");
                        if (i < strings.length) {
                            String size = strings[i];
                            String productSid = presetProductMap.get(size);
                            if (HlpUtils.isEmpty(productSid)) {
                                continue;
                            }
                            List<JxcSaleOrderDetailExt> detailExts = null;//ListUtil.filter(filterDetailList, t -> productSid.equals(t.getProduct_sid()));
                            if (HlpUtils.isEmptyList(detailExts)) {
                                continue;
                            }
                            double totalQty = 0D;
                            for (JxcSaleOrderDetailExt detailExt : detailExts) {
                                totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                            }
                            qtyStrList.add(BigDecimal.valueOf(totalQty).stripTrailingZeros().toPlainString());
                        }
                    }
                    presetQtyMap.put("preset_qty_" + i, StringUtils.join(qtyStrList, "/"));
                }
                detailExportVo.setPreset_qty_map(presetQtyMap);
                double totalQty = 0D, totalAmount = 0D;
                for (JxcSaleOrderDetailExt detailExt : filterDetailList) {
                    totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                    totalAmount = DoubleUtil.add(totalAmount, detailExt.getAmount());
                }
                detailExportVo.setQty(BigDecimal.valueOf(totalQty));
                detailExportVo.setAmount(BigDecimal.valueOf(totalAmount));
                voList.add(detailExportVo);
            }
        }
        voList = ListUtil.sorted(voList, Comparator.comparing(JxcSaleOrderDetailExportVo::getSn));
        for (int i = 0; i < voList.size(); i++) {
            voList.get(i).set_index(i + 1);
        }
        exportVo.setDetail_list(voList);
        return exportVo;
    }
}
